// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#include "MgcBezierTriangle.h"
#include "MgcTriMesh.h"

MgcImplementRTTI(MgcBezierTriangle,MgcBezierPatch);
MgcImplementStream(MgcBezierTriangle);

//----------------------------------------------------------------------------
MgcBezierTriangle::MgcBezierTriangle (unsigned int uiDegree,
    unsigned int uiIndexQuantity, unsigned int* auiIndex)
    :
    MgcBezierPatch(uiDegree,uiIndexQuantity,auiIndex)
{
}
//----------------------------------------------------------------------------
MgcBezierTriangle::MgcBezierTriangle ()
{
}
//----------------------------------------------------------------------------
void MgcBezierTriangle::GenerateConnectivity (unsigned int uiLevel,
    MgcTriMesh* pkMesh, unsigned int& ruiTriangleStart)
{
    // generate connectivity array and put in proper location in trimesh

    unsigned int uiTwoPowL = 1 << uiLevel;
    unsigned int uiTwiceTwoPowLPlusOne = (uiTwoPowL + 1) << 1;
    unsigned int* puiConnect = pkMesh->Connectivity() + 3*ruiTriangleStart;

    for (unsigned int uiY = 0, uiYStart = 0; uiY < uiTwoPowL; uiY++)
    {
        unsigned int uiI0 = uiYStart;
        unsigned int uiI1 = uiI0 + 1;
        uiYStart = ((uiY + 1)*(uiTwiceTwoPowLPlusOne - uiY)) >> 1;
        unsigned int uiI2 = uiYStart;
        unsigned int uiSum;
        for (unsigned int uiX = 0; (uiSum = uiX + uiY) < uiTwoPowL; uiX++)
        {
            *puiConnect++ = uiI0;
            *puiConnect++ = uiI1;
            *puiConnect++ = uiI2;
            ruiTriangleStart++;

            if ( uiSum + 1 < uiTwoPowL )
            {
                *puiConnect++ = uiI1;
                *puiConnect++ = uiI2 + 1;
                *puiConnect++ = uiI2;
                ruiTriangleStart++;
            }

            uiI0++;
            uiI1++;
            uiI2++;
        }
    }
}
//----------------------------------------------------------------------------

//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* MgcBezierTriangle::Factory (MgcStream& rkStream)
{
    // MgcBezierTriangle is abstract, Factory never called
    return 0;
}
//---------------------------------------------------------------------------
void MgcBezierTriangle::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcBezierPatch::Load(rkStream,pkLink);
}
//---------------------------------------------------------------------------
void MgcBezierTriangle::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcBezierPatch::Link(rkStream,pkLink);
}
//---------------------------------------------------------------------------
bool MgcBezierTriangle::Register (MgcStream& rkStream)
{
    return MgcBezierPatch::Register(rkStream);
}
//---------------------------------------------------------------------------
void MgcBezierTriangle::Save (MgcStream& rkStream)
{
    MgcBezierPatch::Save(rkStream);
}
//---------------------------------------------------------------------------
